import 'package:permission_handler/permission_handler.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';
/*
权限管理: ANDROID IOS WINDOWS Web
https://pub.flutter-io.cn/packages/permission_handler

dependencies:
  permission_handler: ^11.3.1

import 'package:permission_handler/permission_handler.dart';


if (await Permission.speech.isPermanentlyDenied) {
  // The user opted to never again see the permission request dialog for this
  // app. The only way to change the permission's status now is to let the
  // user manually enable it in the system settings.
  openAppSettings();
}


android原生清单配置文件:
<!--允许写入存储卡-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:supportsRtl="true"
android:largeHeap="true"
android:hardwareAccelerated="true"
android:usesCleartextTraffic="true"
android:persistent="true"
android:requestLegacyExternalStorage="true"
android:networkSecurityConfig="@xml/network_security_config"
>
 */
abstract class XPermissionUtils{



  //1.申请多个权限
  static void requestAllPermissions(
      {
        XFunctionResultCallback? callback,
      }
      ) async{

    if(XFlutterUtils.isMacOS()){
      return;
    }

    [
      Permission.bluetooth,
      Permission.bluetoothAdvertise,
      Permission.bluetoothConnect,
      Permission.bluetoothScan,
      Permission.camera,
      Permission.phone,
      Permission.microphone,
      Permission.sensors,
      //Permission.requestInstallPackages,
      //Permission.speech,//录音
      Permission.audio,
      Permission.videos,
      Permission.mediaLibrary,
      Permission.photos,
      Permission.notification,
      Permission.accessMediaLocation,
      Permission.accessNotificationPolicy,
    ].request().then((Map<Permission, PermissionStatus> statuses){

      statuses.forEach((Permission key, PermissionStatus value) {
        XLogUtils.printLog('---1.申请权限: $key, $value');
      });

      //2.需要先location授权,再locationAlways授权 (蓝牙扫描需要打开定位)
      [
        Permission.location,
        Permission.locationWhenInUse,
      ].request().then((Map<Permission, PermissionStatus> statuses){

        statuses.forEach((Permission key, PermissionStatus value) {
          XLogUtils.printLog('---2.申请定位权限: $key, $value');
        });

        //3.
        [
          Permission.locationAlways,
        ].request().then((Map<Permission, PermissionStatus> statuses){

          statuses.forEach((Permission key, PermissionStatus value) {
            XLogUtils.printLog('---3.申请定位权限: $key, $value');
          });

          //4.访问文件夹权限
          [
            Permission.storage,
            Permission.manageExternalStorage,
          ].request().then((Map<Permission, PermissionStatus> statuses){

            statuses.forEach((Permission key, PermissionStatus value) {
              XLogUtils.printLog('---4.访问文件夹权限: $key, $value');
            });

            //最后回调,用于外部安装权限等(android 13+的安装权限需要使用xsdk_flutter_plugin)
            if(callback!=null){
              callback('ok');
            }

          });

        });

      });


    });





  }


  //2.申请单个权限 Permission.requestInstallPackages,
  static void requestPermission(Permission permission) async{

    if(XFlutterUtils.isMacOS() ){
      return;
    }

    permission.request().then((PermissionStatus value){
      XLogUtils.printLog('---申请权限: $permission, $value');
    });
  }


}
